package com.xgz.controller;

import com.github.pagehelper.PageInfo;
import com.xgz.entity.LayData;
import com.xgz.entity.MyPage;
import com.xgz.entity.Scl;
import com.xgz.entity.TUser;
import com.xgz.service.SclService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * (Scl)表控制层
 *
 * @author makejava
 * @since 2022-10-26 16:12:33
 */
@Controller
@RequestMapping("scl")
public class SclController {
    /**
     * 服务对象
     */
    @Autowired
    private SclService sclService;

    /**
     * 分页查询所有题目
     * @param myPage
     * @param scl
     * @return
     */
    @RequestMapping("queryAll")
    @ResponseBody
    public LayData queryAll(MyPage myPage, Scl scl){
        PageInfo pageInfo = sclService.queryAll(myPage,scl);
        return new LayData(pageInfo);
    }

    /**
     * 模糊查询
     * @param myPage
     * @param scl
     * @return
     */
    @GetMapping("querysclAll")
    @ResponseBody
    public LayData querysclAll(MyPage myPage, Scl scl){
        PageInfo pageInfo = sclService.querysclAll(myPage,scl);
        System.out.println("pageInfo = " + pageInfo.toString());
        return new LayData(pageInfo);
    }


    /**
     * 题目失焦事件
     * @param sclname
     * @return
     */
    @RequestMapping("queryByName/{sclname}")
    @ResponseBody
    public LayData queryByName(@PathVariable String sclname){
        boolean b = sclService.queryByName(sclname);
        //System.out.println("b = " + b);
        return new LayData(b);
    }

    /**
     * 添加、修改题目
     * @param scl
     * @return
     */
    @RequestMapping("addQuestion")
    @ResponseBody
    public LayData addQuestion(Scl scl){
        //System.out.println("前台传过来的参数是:"+scl);
        boolean b = sclService.addQuestion(scl);
        System.out.println("b = " + b);
        return new LayData(b);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delQuestion")
    @ResponseBody
    public LayData delQuestion(Integer id,Integer sclno){
       /* System.out.println("前台传过来的参数id: = "+id);
        System.out.println("前台传过来的参数sclno: = " + sclno);*/
        boolean b = sclService.delScl(id,sclno);
        return new LayData(b);
    }

    /**
     * 批量删除
     * @param id
     * @param sclno
     * @return
     */
    @RequestMapping("delSclbatch")
    @ResponseBody
    public LayData delSclbatch(@RequestParam Integer[] id, @RequestParam Integer[] sclno){
        System.out.println("前台传过来的参数id: = "+id);
        System.out.println("前台传过来的参数sclno: = " + sclno);
        boolean b = sclService.delSclbatch(id, sclno);
        return new LayData(b);
    }

    /**
     * scldemo页面数据
     * @param myPage
     * @param request
     * @return
     */
    @GetMapping("getsclAll")
    public String getsclAll(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(90);
        myPage.setPage(1);
        PageInfo all = sclService.getAll(myPage);
        //System.out.println("all = " + all.toString());
        request.setAttribute("allsub",all);
        return "scldemo";
    }

    /**
     * 批量添加题目
     */
    @Resource
    private SclService fileService;
    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        System.out.println("file = " + file);
        try {
            fileService.dealFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new LayData(true);
    }

}

